//
//  XiaofeiViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/19.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "XiaofeiViewController.h"

#import "XiaofeiTableViewCell.h"
#import "XiaofeiDetailViewController.h"
#import "TuBiaoZhuViewController.h"
@interface XiaofeiViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

//头视图
@property (nonatomic, strong) UIView * view_head;

@property (nonatomic, strong) UILabel * koufei_1;
@property (nonatomic, strong) UILabel * koufei_2;
@property (nonatomic, strong) UILabel * koufei_3;


//测试
@property (nonatomic, assign) NSInteger pageNumber;

@end

@implementation XiaofeiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"扣费记录"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
//    [self addRightButton:@"shuaxin@2x.png"];
//    _imgRight.frame = CGRectMake(SCREEN_WIDTH - 5 - 30, _imgRight.frame.origin.y - 1.5, _imgRight.frame.size.width, _imgRight.frame.size.height);
//    _btnRight.frame = CGRectMake(SCREEN_WIDTH - 10 - 40, _lblRight.frame.origin.y + 0, 40, _lblRight.frame.size.height - 10);
    
    _btnRight.hidden = YES;
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

////刷新
//- (void)clickRightButton:(UIButton *)sender
//{
////    NSLog(@"重新加载一遍数据,给动画");
//    // 马上进入刷新状态
//    [self.tableView.mj_header beginRefreshing];
//}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self p_headView];
    self.tableView.tableHeaderView = self.view_head;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[XiaofeiTableViewCell class] forCellReuseIdentifier:@"cell_koufei"];
    
    //刷新
    __unsafe_unretained UITableView *tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
    // 马上进入刷新状态
    [tableView.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
//    // 上拉加载
//    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        
//        // 结束刷新
//        [tableView.mj_footer endRefreshing];
//    }];
}

#pragma mark - 头视图
- (void)p_headView
{
    CGFloat length_x = (SCREEN_WIDTH - 10) / 4;

    self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 175)];
    self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    

    UILabel * text = [[UILabel alloc] initWithFrame:CGRectMake(15 , 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
    text.text = @"扣费分析";
    text.font = [UIFont systemFontOfSize:14];
    text.textColor = [UIColor blackColor];
    [self.view_head addSubview:text];
    
    //下面的数据展示
    UIImageView * view_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 100)];
    //            view_bg.image = [UIImage imageNamed:@"whith_bg.png"];
    view_bg.backgroundColor = [UIColor whiteColor];
    [self.view_head addSubview:view_bg];
    
    //1
    UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
    image.image = [UIImage imageNamed:@"tongji@2x.png"];
    [view_bg addSubview:image];
    
    //2
    self.koufei_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x - 5, 30, length_x + 8, 25)];
    self.koufei_1.text = @"￥28.22";
    self.koufei_1.font = [UIFont systemFontOfSize:15];
//    self.koufei_1.backgroundColor = [UIColor orangeColor];
    self.koufei_1.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_1];
    
    UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_1.text = @"昨日扣费";
    label_1.font = [UIFont systemFontOfSize:14];
    label_1.textAlignment = NSTextAlignmentCenter;
    label_1.textColor = [UIColor grayColor];
    [view_bg addSubview:label_1];
    
    
    //3
    self.koufei_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
    self.koufei_2.text = @"￥892";
    self.koufei_2.font = [UIFont systemFontOfSize:15];
//    self.koufei_2.backgroundColor = [UIColor orangeColor];
    self.koufei_2.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_2];
    
    UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_2.text = @"扣费总额";
    label_2.font = [UIFont systemFontOfSize:14];
    label_2.textAlignment = NSTextAlignmentCenter;
    label_2.textColor = [UIColor grayColor];
    [view_bg addSubview:label_2];
    
    
    //4
    self.koufei_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x, 25)];
    self.koufei_3.text = @"￥81";
    self.koufei_3.font = [UIFont systemFontOfSize:15];
    self.koufei_3.textColor = [UIColor redColor];
    self.koufei_3.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_3];
    
    UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_3.text = @"账户余额";
    label_3.font = [UIFont systemFontOfSize:14];
    label_3.textAlignment = NSTextAlignmentCenter;
    label_3.textColor = [UIColor grayColor];
    [view_bg addSubview:label_3];
    
    
    UILabel * text_1 = [[UILabel alloc] initWithFrame:CGRectMake(15 , CGRectGetMaxY(view_bg.frame) + 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
    text_1.text = @"昨日扣费记录";
    text_1.font = [UIFont systemFontOfSize:14];
    text_1.textColor = [UIColor blackColor];
    [self.view_head addSubview:text_1];
    
    
    //手势
    view_bg.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [view_bg addGestureRecognizer:tapGesture];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XiaofeiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_koufei" forIndexPath:indexPath];
    
    //赋值的时候空3格
    cell.text_1.text = [NSString stringWithFormat:@"   关键词%d",indexPath.row + 1];
    cell.text_5.text = @"   zhishun.net";
    cell.text_2.text = [NSString stringWithFormat:@"%d",indexPath.row + 7];
    cell.text_3.text = [NSString stringWithFormat:@"￥ %d",indexPath.row + 15];
    cell.text_4.text = @"12-28";
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    XiaofeiDetailViewController * xiaofeiDetailViewController = [[XiaofeiDetailViewController alloc] init];
    [self showViewController:xiaofeiDetailViewController sender:nil];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //总View
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //扣费记录列表
    UIView * view_bg1 = [[UIView alloc] initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH, 35)];
    view_bg1.backgroundColor = navi_bar_bg_color;
    
    
    CGFloat list_x = (SCREEN_WIDTH - 10) / 6;
    
    UILabel * list_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 3, 20)];
    list_1.text = @"   关键词";
    list_1.textColor = [UIColor whiteColor];
    list_1.font = [UIFont systemFontOfSize:13];
    //    list_1.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:list_1];
    
    
    UILabel * list_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 3, 7.5, list_x * 1, 20)];
    list_2.text = @"排名";
    list_2.textColor = [UIColor whiteColor];
    list_2.font = [UIFont systemFontOfSize:13];
    list_2.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:list_2];
    
    
    UILabel * list_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 4, 7.5, list_x * 1, 20)];
    list_3.text = @"扣费";
    list_3.textColor = [UIColor whiteColor];
    list_3.font = [UIFont systemFontOfSize:13];
    list_3.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:list_3];
    
    
    UILabel * list_4 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 5, 7.5, list_x * 1, 20)];
    list_4.text = @"日期";
    list_4.textColor = [UIColor whiteColor];
    list_4.font = [UIFont systemFontOfSize:13];
    list_4.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:list_4];
    
    [view addSubview:view_bg1];
    
    return view;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 36.0f;
}

#pragma mark - 图表视图
- (void)tapGesture:(id)sender
{
//    TuBiaoZhuViewController * tuBiaoZhuViewController = [[TuBiaoZhuViewController alloc] init];
//    [self showViewController:tuBiaoZhuViewController sender:nil];
}


@end
